package com.chushouya.order.constants;


import com.general.framework.core.enums.IEnum;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Strings;

import java.util.Date;
import java.util.Objects;

/**
 * 统计时间范围
 */
public enum DataScopeEnum implements IEnum<String> {

    today("today", "今日数据"),
    yesterday("yesterday", "昨日数据"),
    week("week", "本周数据"),
    month("month", "本月数据"),
    total("total", "累积数据");

    private String value;

    private String desc;

    DataScopeEnum(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    public static DataScopeEnum get(String value) {
        for (DataScopeEnum thisEnum : DataScopeEnum.values()) {
            if (Objects.equals(thisEnum.value, value)) {
                return thisEnum;
            }
        }
        throw Ex.systemError("未定义的枚举[{}]", value);
    }

    public static String getDesc(String value) {
        for (DataScopeEnum thisEnum : DataScopeEnum.values()) {
            if (Objects.equals(thisEnum.value, value)) {
                return thisEnum.desc;
            }
        }
        return Strings.EMPTY;
    }

    @Override
    public String value() {
        return this.value;
    }

    @Override
    public String named() {
        return this.desc;
    }

    public Date timeStart() {
        switch (this) {
            case today:
                return Dates.getFirstTime(Dates.getTimeNow());
            case yesterday:
                return Dates.getFirstTime(Dates.addDays(Dates.getTimeNow(), -1));
            case week:
                return Dates.getFirstTime(Dates.getWeekFirstTime(Dates.getTimeNow()));
            case month:
                return Dates.getMonthFirstTime(Dates.getTimeNow());
            case total:
                return Dates.parseDate("2000-01-01");
        }
        throw Ex.systemError("系统错误");
    }

    public Date timeEnd() {
        switch (this) {
            case today:
                return Dates.getLastTime(Dates.getTimeNow());
            case yesterday:
                return Dates.getLastTime(Dates.addDays(Dates.getTimeNow(), -1));
            case week:
                return Dates.getLastTime(Dates.getWeekLastTime(Dates.getTimeNow()));
            case month:
                return Dates.getMonthLastTime(Dates.getTimeNow());
            case total:
                return Dates.parseDate("2099-01-01");
        }
        throw Ex.systemError("系统错误");
    }

}
